<?php
namespace Admin\Controller;

class BankruleController extends AdminController
{

	public $carModels     = ['新车','二手车','存量车','商用车'];//车型构成
	public $yearLimits    = [12 =>'一年', 24 => '二年', 36 => '三年', 48 => '四年', 60 => '五年'];//年限
	public $financeRanges = [1 =>'车价',  2 => '购置税', 4 => '保险',  8 => 'GPS', 16 => '装潢'];//位运算计入

    protected function _initialize()
    {
		parent::_initialize ();
	}

	public function add()
	{
		if (IS_POST)
		{
			$fRate        = floatval($_POST['rate']);
			$nIs_province = $_POST['is_province'];

			if (empty($fRate) || $fRate == 0)
			{
				$this->error('费率未填写或者数据类型不符合');
			}

			if (is_null($nIs_province))
			{
				$this->error('请选择一项省内外');
			}


			$_totalFinace = 0;
			foreach ($_POST['finance_range'] as $value)
			{
				$_totalFinace += $value;
			}

          	$data = [
				'rate'          => $fRate,
				'bank_id'       => $_POST['bank_id'],
				'car_model'     => $_POST['car_model'],
				'year_limit'    => $_POST['year_limit'],
				'is_province'   => $nIs_province,
				'finance_range' => $_totalFinace,
				];

			$result = $this->updateData($data, 'bank_rule');
			if($result)
			{
				$this->success('添加成功',U('Bankrule/index'));
			}else{
				return $this->error('添加失败');
			}
		}


		$_bankList = $this->getList('*','bank','status = 0','id desc');
		$this->assign('carModels',$this->carModels);
		$this->assign('yearLimits',$this->yearLimits);
		$this->assign('financeRanges',$this->financeRanges);
		$this->assign('_bankList',$_bankList);
		$this->assign('_departList',$_departList);
		$this->display();
	}

	public function edit()
	{
		if (IS_POST)
		{
			$_totalFinace = 0;
			foreach ($_POST['finance_range'] as $value)
			{
				$_totalFinace += $value;
			}

          	$data = [
				'rate'          => $_POST['rate'],
				'bank_id'       => $_POST['bank_id'],
				'car_model'     => $_POST['car_model'],
				'year_limit'    => $_POST['year_limit'],
				'is_province'   => $_POST['is_province'],
				'finance_range' => $_totalFinace,];

	  		$result = M('bank_rule')->where('id='.I('post.id'))->save($data);
	  		if ($result)
	  		{
	  			$this->redirect('Bankrule/index');
	  		}
		} else {
			$info        = $this->getInfo('*','bank_rule','id='.I('get.id',0,'intval'));
			$_bankList   = $this->getList('*','bank','status = 0','id desc');

			$ttA = [1,2,4,8,16,32,64];
			$_tmp = [];
			foreach ($ttA as $key => $_v)
			{
				if (($_v&$info['finance_range']) == $_v)
				{
					array_push($_tmp , $_v);
				}
			}

			$this->assign('info',$info);
			$this->assign('tmpFR',$_tmp);
			$this->assign('_bankList',$_bankList);
			$this->assign('carModels',$this->carModels);
			$this->assign('yearLimits',$this->yearLimits);
			$this->assign('financeRanges',$this->financeRanges);
			$this->display('edit');
		}

	}

	public function index($p = 1)
	{
		$p        = intval($p) > 0 ? $p : 1;
        $bankRule = M('bank_rule br');
        $pagesize = 15;#每页数量
        $offset   = $pagesize * ($p - 1);//计算记录偏移量

		$where =' 1 = 1 ';
		$where .= I('get.bank_id') != '' ? ' and br.bank_id = ' . I('get.bank_id')  : '';
		$where .= I('get.car_model') != '' ? ' and br.car_model = ' . I('get.car_model') : '';
		$where .= I('get.is_province') != '' ? ' and br.is_province = ' .I('get.is_province') : '';
		$where .= I('get.year_limit') != '' ? ' and br.year_limit = ' . I('get.year_limit') : '';
		$where .= ' and br.status = 0 ';

		$count = $bankRule->join('join app_bank b on b.id = br.bank_id')->where($where)->count();

        $list  = $bankRule->field('br.*,b.bank_name')->join('join app_bank b on b.id = br.bank_id')->where($where)->order(' br.id desc ')->limit($offset . ',' . $pagesize)->select();
        $page  = new \Think\Page($count, $pagesize);
        $page  = $page->show();

		$bankList= $this->getList('*','bank','status = 0','id desc');
		$this->assign('page',$page);
		$this->assign('list',$list);
		$this->assign('bankList',$bankList);
		$this->assign('carModelItems', $this->carModelItems);
		$this->assign('yearLimitItems', $this->yearLimitItems);
		$this->display();
	}

	public function detail()
	{
		$this->display();
	}

	public function add_info()
	{
		$this->display();
	}

	public function Rebate()
	{
		$this->display();
	}

	public function Complain_index()
	{
		$this->display();
	}

	public function Complain_deal()
	{
		$this->display();
	}

	public function Complain_del()
	{
		$this->display();
	}
}